package com.hake.test.jedis;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import com.alibaba.fastjson.JSON;
import com.hake.core.Constants;
import com.hake.core.exception.AppException;
import com.hake.core.util.ProtoStuffSerializerUtil;
import com.hake.manager.system.entity.User;
import com.hake.manager.system.service.UserService;
import com.hake.test.BaseJunit;

public class JedisUtil extends BaseJunit {

	private Jedis jedis ;
	
	private JedisPool jedisPool ;
	
	@Autowired
	private UserService userService ;
	
	@Before
	public void before() {
		if(null == jedis) {
			jedisPool = new JedisPool("127.0.0.1") ;
			jedis = jedisPool.getResource() ;
		}
	}
	
	@Test
	public void testString() {
		System.out.println(jedis);
		jedis.set("name", "yhaoquan") ;
		jedis.set("name", "123456") ;
		
		jedis.mset("a", "aa", "age", "12", "c", "cc") ;
		jedis.incrBy("age", 1) ;
		
		
		System.out.println(jedis.get("name"));
		System.out.println(jedis.mget("a") +"--" + jedis.mget("age") +"--" + jedis.mget("c"));
	}
	
	@Test
	public void testMap() {
		Map<String, String> map = new HashMap<String, String>() ;
		map.put("name", "yhq") ;
		map.put("age", "28") ;
		map.put("address", "广州") ;
		
		jedis.hmset("user", map) ;
		
		System.out.println(jedis.hmget("user", "name", "age", "address"));
		
		Map<String, String> hgetAll = jedis.hgetAll("user") ;
		for(String key : hgetAll.keySet()) {
			System.out.println(jedis.hmget("user", key));
		}
		Iterator<String> iterator = jedis.hkeys("user").iterator() ;
		while (iterator.hasNext()) {
			String key = (String) iterator.next();
			System.out.println(jedis.hmget("user", key));
		}
	}
	
	@Test
	public void testList() {
		jedis.del("list") ;
		jedis.lpush("list", "list1");
		jedis.lpush("list", "list2");
		jedis.lpush("list", "list3");
		
		List<String> lrange = jedis.lrange("list", 0, jedis.llen("list")) ;
		for (String s : lrange) {
			System.out.println(s);
		}
	}
	
	@Test
	public void testObject() {
		try {
			List<User> selectAll = this.userService.selectAll() ;
			
			byte[] bytes = BytesUtil.toBytes(selectAll) ;
			System.out.println(bytes);
			
			User user = this.userService.selectByPrimaryKey(633) ;
			jedis.set(BytesUtil.keyToBytes("user:1"), BytesUtil.toBytes(user)) ;
			byte[] bs = jedis.get(BytesUtil.keyToBytes("user:1")) ;
			System.out.println(JSON.toJSON(BytesUtil.bytesToObject(bs, User.class)));
			
			
			byte[] serializeList = ProtoStuffSerializerUtil.serializeList(selectAll) ;
			System.out.println(serializeList);
			List<User> deserializeList = ProtoStuffSerializerUtil.deserializeList(serializeList, User.class) ;
			for (User user2 : deserializeList) {
				System.out.println(JSON.toJSON(user2));
			}
			
		} catch (AppException e) {
			e.printStackTrace();
		}
		
		jedisPool.close();
	}
	
	@Test
	public void testMenu() {
		ProtoStuffSerializerUtil.serialize(Constants.CACHE_NAMESPACE + "selectMenuByCategoryForCache");
	}
	
	@Test
	public void test03() {
		Map<byte[], byte[]> map = new HashMap<byte[], byte[]>() ;
		map.put(ProtoStuffSerializerUtil.serialize("name"), ProtoStuffSerializerUtil.serialize("yhq")) ;
		map.put(ProtoStuffSerializerUtil.serialize("age"), ProtoStuffSerializerUtil.serialize("28")) ;
		map.put(ProtoStuffSerializerUtil.serialize("address"), ProtoStuffSerializerUtil.serialize("广州")) ;
		
		
		/*jedis.hmset(ProtoStuffSerializerUtil.serialize("user"), map) ;
			
		Map<byte[], byte[]> hgetAll = jedis.hgetAll(ProtoStuffSerializerUtil.serialize("user")) ;
		for(byte[] key : hgetAll.keySet()) {
			String k = ProtoStuffSerializerUtil.deserialize(key, String.class) ;
			System.out.println(k +"=="+ ProtoStuffSerializerUtil.deserialize(hgetAll.get(key), String.class));
		}*/
		
		/*List<byte[]> hmget = jedis.hmget(ProtoStuffSerializerUtil.serialize("user")) ;
		
		for (byte[] bs : hmget) {
			String deserialize = ProtoStuffSerializerUtil.deserialize(bs, String.class) ;
			System.out.println(deserialize);
		}*/
		
	}
	
	/*public static void main(String[] args) {
		JedisUtil jedisUtil = new JedisUtil("127.0.0.1", 6379) ;
		
		jedisUtil.testString();
		jedisUtil.testMap();
		jedisUtil.testList();
	}*/
	
}
